<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>内部通知</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="通知名称:">
            <el-input v-model="editForm.title" auto-complete="off" placeholder="填写卷目名"></el-input>
          </el-form-item>
          <el-form-item label="通知描述:">
            <el-input v-model="editForm.body" auto-complete="off" placeholder="补充用途" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="签收时间:" prop="name">
            <el-date-picker type="date"
                            placeholder="签收时间"
                            v-model="editForm.time"
                            :picker-options="pickerOptions1"
                            format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="通知人员:" prop="name">
            <el-button type="primary" @click="chooseStudent" class="mb-10" size="small" plain>选择通知人员</el-button>
            <el-button @click.native="groupVisible=true" size="small">选择通知群组（多选）</el-button>
            <div>
              <el-tag
                  :key="tag.id"
                  v-for="(tag,index) in dynamicTags"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(index)"
                  class="mr-10"
              >{{ tag.realname }}
              </el-tag>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click="editSubmit">确定</el-button>
      </div>
    </div>
    <MultipleTeacherModle :visible.sync="studentVisible" @studentFn="studentFn"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
    <!--    选择群组-->
    <groupModule :visible.sync="groupVisible" @studentFn="studentFn"></groupModule>
  </div>
</template>

<script>
import {notice_inside_add, notice_inside_detail, notice_inside_edit} from '@/api/admin/notice'

export default {
  props: ['visible', 'sealId', 'type'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      editForm: {
        title: '',
        time: '',
        type: '', // 	整型 	必填 	1 	- 	1:预通知;2:审发通知
        body: '',
        in_user: ''
      }, // 编辑界面数据
      // 限制时间选择
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
      studentVisible: false,
      groupVisible: false,
      dynamicTags: [],
      dynamicTagsArray: {}
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.sealId) {
          // this.getDetail();
        }
      } else {
        this.clearnFn()
      }
    }
  },
  created() {
  },
  methods: {
    handleClose(index) {
      this.dynamicTags.splice(index, 1)
    },
    getDetail() {
      files_Detail({id: this.sealId}).then(res => {
        console.log('res:', res)
      })
    },
    chooseStudent() {
      let list = {}
      for (let item of this.dynamicTags) {
        list[item.id] = item
      }
      this.dynamicTagsArray = this.getJson(list)
      this.studentVisible = true
    },
    studentFn(option) {
      let list = [];
      for (let i in option) {
        list.push(option[i])
      }
      this.dynamicTags = this.getJson(list)
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 提交数据
    editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.title) {
        this.$message.error('填写通知名称')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('选择通知人员')
        return
      }
      let a_ = []
      for (let item of this.dynamicTags) {
        a_.push(item.id)
      }
      dataOption.type = this.type
      dataOption.in_user = a_.join(',')
      // !files_add, files_Detail, files_edit
      if (!this.sealId) {
        notice_inside_add(dataOption).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('InitFn')
            this.closePOpup()
            this.clearnFn()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        notice_inside_edit(dataOption).then(res => {
          console.log('res:', res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('InitFn')
            this.closePOpup()
            this.clearnFn()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    clearnFn() {
      this.editForm = {
        title: '',
        time: '',
        type: '', // 	整型 	必填 	1 	- 	1:预通知;2:审发通知
        body: '',
        in_user: ''
      } // 编辑界面数据
      this.dynamicTags = []
      this.dynamicTagsArray = {}
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


